package com.example.test_aidl;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentPagerAdapter;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Ohuang
 */
public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private IMyAidlInterface userlmpl;
    private List<User> userList = new ArrayList<>();
    private List<User> seriveList = new ArrayList<>();

    ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            userlmpl = IMyAidlInterface.Stub.asInterface(service);
            try {
                userlmpl.addAidlInterface(aidlInterface);
                getUser1=userlmpl.addUserIn(user1);
                getUser2=userlmpl.addUserOut(user2);
                getUser3=userlmpl.addUserInout(user3);
                seriveList.add(getUser1);
                seriveList.add(getUser2);
                seriveList.add(getUser3);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "onCreate: ");
        //异步绑定
        Intent intent = new Intent(this, MyService.class);
        bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
        user1 = new User(1, "tom1", "11");
        user2 = new User(2, "tom2", "11");
        user3 = new User(3, "tom3", "11");

        userList.add(user1);
        userList.add(user2);
        userList.add(user3);

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            userlmpl.removeAidlInterface(aidlInterface);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        unbindService(serviceConnection);
    }

    private User user1;
    private User user2;
    private User user3;


    private User getUser1;
    private User getUser2;
    private User getUser3;
    private AidlInterface aidlInterface=new AidlInterface.Stub(){

        @Override
        public void onMessge(String msg) throws RemoteException {
            Log.e(TAG, "onMessge: "+msg);
        }
    };
    public void sendData(View view) throws RemoteException {

        userlmpl.setUsersMsg("沒錢了");


    }

    public void getData(View view) throws RemoteException {
//        user1.setName("cat1");
//        user2.setName("cat2");
//        user3.setName("cat3");
        user3.setName("小鸡");
    }

    public void Data(View view) throws RemoteException {

        Log.e(TAG, "Data: "+(user3==getUser3));
        Log.e(TAG, "Data: userList"+userList.toString());
        Log.e(TAG, "Data: seriveList" +seriveList.toString() );
        Log.e(TAG, "Data: userlmpl" + userlmpl.getUsers().toString());
    }


}